//package com.lagou.zookeeper;
//
//import com.lagou.client.RPCConsumer;
//import com.lagou.service.IUserService;
//import org.apache.zookeeper.KeeperException;
//import org.apache.zookeeper.WatchedEvent;
//import org.apache.zookeeper.Watcher;
//import org.apache.zookeeper.ZooKeeper;
//import org.apache.zookeeper.data.Stat;
//
//import java.io.IOException;
//import java.io.UnsupportedEncodingException;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//import java.util.Random;
//
///**
// * @program: stage-3-module-2
// * @description:
// * @author: zhangxiaoyang
// * @date: 2020-08-23 22:36
// **/
//public class LoadBalanceGetNodeData implements Watcher {
//    private static final String PROVIDER_NAME = "UserService#sayHello#";
//
//
//    public static ZooKeeper zooKeeper;
//
//    private RPCConsumer rpcConsumer;
//
//
//    public static Map<String, Long> addressMap = new HashMap<>();
//
//    public RPCConsumer getRpcConsumer() {
//        return rpcConsumer;
//    }
//
//    public static void init() throws IOException {
//        zooKeeper = new ZooKeeper(ZkConstant.ZK_SERVER + ":" + ZkConstant.ZK_PORT, 50000, new LoadBalanceGetNodeData());
//        System.out.println("消费者客户端zk连接成功！！！");
//    }
//
//    @Override
//    public void process(WatchedEvent watchedEvent) {
//        if (watchedEvent.getType() == Event.EventType.NodeChildrenChanged) {
//            try {
//                List<String> children = getChildren();
////                System.out.println("子节点列表：" + children);
////                int minIndex = 0;
//
//                for (int i = 0; i < children.size(); i++) {
//                    if (addNodeData(children.get(i))) {
////                        minIndex = loadBalance(children, i);
//                        String[] arr = children.get(i)
//                                               .split(":");
//                        try {
//                            this.connect(arr[0], Integer.valueOf(arr[1]));
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        } catch (NumberFormatException e) {
//                            e.printStackTrace();
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }
//                    }
//
//                }
////                String[] arr = children.get(minIndex)
////                                       .split(":");
//
//
////                for (Map.Entry<String, Long> entry : addressMap.entrySet()) {
////                    Stat stat = zooKeeper.exists(ZkConstant.ROOT_NODE + "/" + entry.getKey(), false);
////                    if (stat == null) {
////                        rpcConsumer.disconnect(entry.getKey());
////                    }
////                }
//            } catch (UnsupportedEncodingException e) {
//                e.printStackTrace();
//            } catch (KeeperException e) {
//                e.printStackTrace();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
//        if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
//            try {
//                List<String> children = getChildren();
//                for (String child : children) {
//                    if (addNodeData(child)) {
//                        String[] arr = child.split(":");
//                        try {
//                            this.connect(arr[0], Integer.valueOf(arr[1]));
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                }
//            } catch (KeeperException e) {
//                e.printStackTrace();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            } catch (UnsupportedEncodingException e) {
//                e.printStackTrace();
//            }
//        }
//        if (watchedEvent.getType() == Event.EventType.NodeDeleted) {
//            try {
//                for (Map.Entry<String, Long> entry : addressMap.entrySet()) {
//                    Stat stat = zooKeeper.exists(ZkConstant.ROOT_NODE + "/" + entry.getKey(), false);
//                    if (stat == null) {
//                        rpcConsumer.disconnect(entry.getKey());
//                    }
//                }
//            } catch (KeeperException e) {
//                e.printStackTrace();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
//
//    }
//
//    private List<String> getChildren() throws KeeperException, InterruptedException {
//        List<String> children = zooKeeper.getChildren(ZkConstant.ROOT_NODE, true);
////        System.out.println("子节点列表：" + children);
//        return children;
//    }
//
//    private boolean addNodeData(String child) throws KeeperException, InterruptedException, UnsupportedEncodingException {
//        byte[] bytes = zooKeeper.getData(ZkConstant.ROOT_NODE + "/" + child, true, null);
//        System.out.println(ZkConstant.ROOT_NODE + "/" + child + "节点数据" + new String(bytes, "UTF-8"));
//        return addressMap.put(child, Long.valueOf(new String(bytes, "UTF-8"))) == null;
//    }
//
//
//    public void connect(String ip, int port) throws IOException, InterruptedException, KeeperException {
//        rpcConsumer = new RPCConsumer();
//        //初始化zk
//        LoadBalanceGetNodeData.init();
//        //初始化客户端
//        rpcConsumer.initClient(ip, port);
//
//    }
//
//
//    private String loadBalance() throws KeeperException, InterruptedException {
//        List<String> children = getChildren();
//        int minIndex = new Random().nextInt(children.size());
//        //负载均衡
//        for (int i = 0; i < children.size(); i++) {
//            if (children.size() == 1) {
//                minIndex = i;
//                return children.get(minIndex);
//            } else {
//                if (addressMap.get(children.get(i)) < addressMap.get(children.get(i + 1))) {
//                    minIndex = i;
//                } else if (addressMap.get(children.get(i)) > addressMap.get(children.get(i + 1))) {
//                    minIndex = i + 1;
//                } else {
//                    return children.get(minIndex);
//                }
//            }
//        }
//        return children.get(minIndex);
//    }
//}
